#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from base.Sequence import Sequence
from base.TestUtils import assert_equal
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU


class MainSequence(Sequence):
    def generate(self, **kargs):

        free_regs_int = self.getRandomRegisters(10, "GPR", "31")
        free_regs_float = self.getRandomRegisters(10, "FPR", "31")

        for reg in free_regs_int:
            self.notice(">>>>>>>>>>>   Int reg id:  {}".format(reg))

        for reg in free_regs_float:
            self.notice(">>>>>>>>>>>   Float reg id:  {}".format(reg))

        for rs1 in free_regs_int:
            record_id = self.genInstruction("ADD##CALM", {"rs1": rs1})
            instr_obj = self.queryInstructionRecord(record_id)
            source_regs = instr_obj["Srcs"]

            if "rs1" in source_regs:
                self.notice(
                    ">>>>>>>>>>>>  Generated instruction and rs1:  "
                    "{}      {}".format(instr_obj["Name"], source_regs["rs1"])
                )
            else:
                self.notice(">>>>>>>>>>>>   RS1 Not Found.   ")

            assert_equal(
                source_regs["rs1"],
                rs1,
                "The generated instruction does not have the requested index value for rs1.",
            )

        for rs1 in free_regs_float:
            record_id = self.genInstruction("FADD.D##CALM", {"rs1": rs1})
            instr_obj = self.queryInstructionRecord(record_id)
            source_regs = instr_obj["Srcs"]

            if "rs1" in source_regs:
                self.notice(
                    ">>>>>>>>>>>>  Generated instruction and rs1:  "
                    "{}      {}".format(instr_obj["Name"], source_regs["rs1"])
                )
            else:
                self.notice(">>>>>>>>>>>>   RS1 Not Found.   ")

            assert_equal(
                source_regs["rs1"],
                rs1,
                "The generated instruction does not have the requested index value for rs1.",
            )


# Points to the MainSequence defined in this file
MainSequenceClass = MainSequence

# Using GenThreadGPGPU by default, can be overriden with extended classes
GenThreadClass = GenThreadGPGPU

# Using EnvGPGPU by default, can be overriden with extended classes
EnvClass = EnvGPGPU
